﻿/*
____
 L|ypps Web controls for ASP.NET

Copyright (c) 2009 Alessandro Zifiglio, http://www.Typps.com
EMail: alessandro@Typps.com

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace Typps.Tests
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    using Typps;

    [TestClass]
    public class UnitTestBCodeParser
    {
        #region Methods

        // Methods
        [TestMethod]
        public void ParseInclusionBTagAllowClassAttribute()
        {
            List<ElementPair> pairs = new List<ElementPair>();
            ElementPair ep = new ElementPair();
            ep.StartTag = "<b>";
            ep.EndTag = "</b>";
            ep.Attributes.Add("class");
            pairs.Add(ep);
            HtmlStripper p = new HtmlStripper(pairs, StripMode.TagAndContent);
            string html = "<b id=\"d\" class=\"yes\">a</b>";
            Assert.IsTrue(p.Parse(html.ToCharArray()).Equals("<b class=\"yes\">a</b>", StringComparison.OrdinalIgnoreCase));
        }

        [TestMethod]
        public void ParseInclusionBTagAllowIdAttribute()
        {
            List<ElementPair> pairs = new List<ElementPair>();
            ElementPair ep = new ElementPair();
            ep.StartTag = "<b>";
            ep.EndTag = "</b>";
            ep.Attributes.Add("id");
            pairs.Add(ep);
            HtmlStripper p = new HtmlStripper(pairs, StripMode.TagAndContent);
            string html = "<b id=\"d\" class=\"yes\">a</b>";
            Assert.IsTrue(p.Parse(html.ToCharArray()).Equals("<b id=\"d\">a</b>", StringComparison.OrdinalIgnoreCase));
        }

        [TestMethod]
        public void ParseInclusionBTagAndContent()
        {
            List<ElementPair> pairs = new List<ElementPair>();
            ElementPair ep = new ElementPair();
            ep.StartTag = "<b>";
            ep.EndTag = "</b>";
            pairs.Add(ep);
            HtmlStripper p = new HtmlStripper(pairs, StripMode.TagOnly);
            string html = "<p>a</p><b>hello</b><div>ok man erhm</div>";
            Assert.IsTrue(p.Parse(html.ToCharArray()).Equals("a<b>hello</b>ok man erhm", StringComparison.OrdinalIgnoreCase));
        }

        [TestMethod]
        public void ParseInclusionBTagAndContentNoAttribute()
        {
            List<ElementPair> pairs = new List<ElementPair>();
            ElementPair ep = new ElementPair();
            ep.StartTag = "<b>";
            ep.EndTag = "</b>";
            pairs.Add(ep);
            HtmlStripper p = new HtmlStripper(pairs, StripMode.TagAndContent);
            string html = "<b id=\"d\" class=\"yes\">a</b>";
            Assert.IsTrue(p.Parse(html.ToCharArray()).Equals("<b>a</b>", StringComparison.OrdinalIgnoreCase));
        }

        [TestMethod]
        public void ParseInclusionBTagOnly()
        {
            List<ElementPair> pairs = new List<ElementPair>();
            ElementPair ep = new ElementPair();
            ep.StartTag = "<b>";
            ep.EndTag = "</b>";
            pairs.Add(ep);
            HtmlStripper p = new HtmlStripper(pairs, StripMode.TagAndContent);
            string html = "<p>a</p><b>hello</b><div>ok man erhm</div>";
            Assert.IsTrue(p.Parse(html.ToCharArray()).Equals("<b>hello</b>", StringComparison.OrdinalIgnoreCase));
        }

        [TestMethod]
        public void ParseInclusionDivTagOnly()
        {
            List<ElementPair> pairs = new List<ElementPair>();
            ElementPair ep = new ElementPair();
            ep.StartTag = "<div>";
            ep.EndTag = "</div>";
            pairs.Add(ep);
            HtmlStripper p = new HtmlStripper(pairs, StripMode.TagOnly);
            string html = "<p>a</p><b>hello</b><div>ok man erhm</div>";
            Assert.IsTrue(p.Parse(html.ToCharArray()).Equals("ahello<div>ok man erhm</div>", StringComparison.OrdinalIgnoreCase));
        }

        [TestMethod]
        public void ParseInclusionDivTagOnlyMissingOpeningTag()
        {
            List<ElementPair> pairs = new List<ElementPair>();
            ElementPair ep = new ElementPair();
            ep.StartTag = "<div>";
            ep.EndTag = "</div>";
            pairs.Add(ep);
            HtmlStripper p = new HtmlStripper(pairs, StripMode.TagOnly);
            string html = "<p>a</p><b>hello</b>ok man erhm</div>";
            Assert.IsTrue(p.Parse(html.ToCharArray()).Equals("ahellook man erhm", StringComparison.OrdinalIgnoreCase));
        }

        [TestMethod]
        public void ParseInclusionNestedBTagAllowIdAttribute()
        {
            List<ElementPair> pairs = new List<ElementPair>();
            ElementPair ep = new ElementPair();
            ep.StartTag = "<b>";
            ep.EndTag = "</b>";
            ep.Attributes.Add("id");
            pairs.Add(ep);
            HtmlStripper p = new HtmlStripper(pairs, StripMode.TagAndContent);
            string html = "<div id=\"a\"><b id=\"d\" class=\"yes\">a</b></div>";
            Assert.IsTrue(p.Parse(html.ToCharArray()).Equals("<b id=\"d\">a</b>", StringComparison.OrdinalIgnoreCase));
        }

        [TestMethod]
        public void ParseInclusionNestedBTagAllowIdAttributeRedudantSpaceToleration()
        {
            List<ElementPair> pairs = new List<ElementPair>();
            ElementPair ep = new ElementPair();
            ep.StartTag = "<b>";
            ep.EndTag = "</b>";
            ep.Attributes.Add("id");
            pairs.Add(ep);
            HtmlStripper p = new HtmlStripper(pairs, StripMode.TagAndContent);
            string html = "<div  id=\"a\"><b  id=\"d\"  class=\"yes\"  >a</b></div>";
            Assert.IsTrue(p.Parse(html.ToCharArray()).Equals("<b id=\"d\">a</b>", StringComparison.OrdinalIgnoreCase));
        }

        [TestMethod]
        public void ParseInclusionSelfClosingBRTag()
        {
            List<ElementPair> pairs = new List<ElementPair>();
            ElementPair ep = new ElementPair();
            ep.StartTag = "<br />";
            pairs.Add(ep);
            HtmlStripper p = new HtmlStripper(pairs, StripMode.TagAndContent);
            string html = "<br />";
            Assert.IsTrue(p.Parse(html.ToCharArray()).Equals("<br />", StringComparison.OrdinalIgnoreCase));
        }

        [TestMethod]
        public void ParseInclusionSelfClosingBRTagAndContentNested()
        {
            List<ElementPair> pairs = new List<ElementPair>();
            ElementPair ep = new ElementPair();
            ep.StartTag = "<br />";
            pairs.Add(ep);
            HtmlStripper p = new HtmlStripper(pairs, StripMode.TagOnly);
            string html = "<d>e<br />jlkjlk</d>";
            Assert.IsTrue(p.Parse(html.ToCharArray()).Equals("e<br />jlkjlk", StringComparison.OrdinalIgnoreCase));
        }

        [TestMethod]
        public void ParseInclusionSelfClosingBRTagExcessiveSpaceTolerance()
        {
            List<ElementPair> pairs = new List<ElementPair>();
            ElementPair ep = new ElementPair();
            ep.StartTag = "<br />";
            pairs.Add(ep);
            HtmlStripper p = new HtmlStripper(pairs, StripMode.TagAndContent);
            string html = "<br   />";
            Assert.IsTrue(p.Parse(html.ToCharArray()).Equals("<br />", StringComparison.OrdinalIgnoreCase));
        }

        [TestMethod]
        public void ParseInclusionSelfClosingBRTagNoSpace()
        {
            List<ElementPair> pairs = new List<ElementPair>();
            ElementPair ep = new ElementPair();
            ep.StartTag = "<br/>";
            pairs.Add(ep);
            HtmlStripper p = new HtmlStripper(pairs, StripMode.TagAndContent);
            string html = "<br />";
            Assert.IsTrue(p.Parse(html.ToCharArray()).Equals("<br />", StringComparison.OrdinalIgnoreCase));
        }

        [TestMethod]
        public void ParseInclusionSelfClosingBRTagOnlyNested()
        {
            List<ElementPair> pairs = new List<ElementPair>();
            ElementPair ep = new ElementPair();
            ep.StartTag = "<br />";
            pairs.Add(ep);
            HtmlStripper p = new HtmlStripper(pairs, StripMode.TagOnly);
            string html = "<div>what's up<br style=\"clear:both;\" /> again content here</div>";
            Assert.IsTrue(p.Parse(html.ToCharArray()).Equals("what's up<br /> again content here", StringComparison.OrdinalIgnoreCase));
        }

        [TestMethod]
        public void ParseInclusionSelfClosingInputTag()
        {
            List<ElementPair> pairs = new List<ElementPair>();
            ElementPair ep = new ElementPair();
            ep.StartTag = "<input />";
            ep.Attributes.Add("type");
            ep.Attributes.Add("value");
            pairs.Add(ep);
            HtmlStripper p = new HtmlStripper(pairs, StripMode.TagAndContent);
            string html = "<input type=\"text\" value=\"hi man\" onclick=\"alert('123')\" />";
            Assert.IsTrue(p.Parse(html.ToCharArray()).Equals("<input type=\"text\" value=\"hi />", StringComparison.OrdinalIgnoreCase));
        }

        [TestMethod]
        public void ParseOnlyInnerTextRemoveMarkup()
        {
            List<ElementPair> pairs = new List<ElementPair>();
            HtmlStripper p = new HtmlStripper(pairs, StripMode.TagOnly);
            string html = "<h1>Heading</h1><div>hello man</div><p>what is going on?</p><p>A paragraph <span>Yayy</span></p>";
            Assert.IsTrue(p.Parse(html.ToCharArray()).Equals("Headinghello manwhat is going on?A paragraph Yayy", StringComparison.OrdinalIgnoreCase));
        }

        #endregion Methods
    }
}